package paylayer

import (
	"strconv"
	"strings"
	"encoding/json"
	"dhfshop/utils/math"
	//"dhfshop/models/isv"
	//wxpay "wxsdk/wxpay/wxapp"
	"github.com/astaxie/beego"
)

type PayInfoCache struct {
	PartnerId  int64    `json:"partner_id"`
	AppId      string   `json:"app_id"`    
	MchId      string   `json:"mch_id"`
	OrderKind  string   `json:"order_kind"`
	AppKey     string   `json:"app_key"`
	Currency   string   `json:"currency"`
	SignInfo   string   `json:"sign_info"`
}

type CommonPayNotify struct {
    PartnerId     int64   `json:"partner_id"`                      
	StoreId       int64   `json:"store_id"`                            
	PayWayId      int     `json:"pay_way_id"`     //支付商编号
	MemberId      int64   `json:"member_id"`     
	PaymentCode   string  `json:"payment_code"`  /*支付编码:1微信支付2余额支付3ROYAL支付4YahandPay*/ 
	AppId         string  `json:"app_id"`                    
	MchId         string  `json:"mch_id"`         //商户号
	OpenId        string  `json:"open_id"`        //用户标识
	OrderId       string   `json:"order_id"`       //商户订单号                    
	TransactionId string  `json:"transaction_id"` //微信支付订单号         
	TotalFee      float32 `json:"total_fee"`      //支付金额，单位元
	CashFee       float32 `json:"cash_fee"`        //现金支付金额，单位元
	CouponFee     float32 `json:"coupon_fee"`      //代金券或立减优惠金额，单位元
	FeeType       string  `json:"fee_type"`       //货币种类, 默认人民币：CNY	
	Kind          string  `json:"kind"`           //业务类型
	TimeEnd       string  `json:"time_end"`       //支付完成时间，格式为yyyyMMddHHmmss
	ResultCode    string  `json:"result_code"`            //SUCCESS/FAIL，业务结果
	ErrCode       string  `json:"err_code,omitempty"`     //错误代码
	ErrCodeDes    string  `json:"err_code_des,omitempty"` //错误代码描述
	SrcMessage    string  `json:"src_message,omitempty"` /* 原始支付单信息  */
	MchTypeId     int     `json:"mch_type_id"`           //微信支付商户类型: 1普通商户;2特约商户3个人收款码模式
}

func MsgPayHandle(reqMap map[string]string) (err error) {
    payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
	    if len(reqMap["sub_mch_id"]) > 0 {    
			payNotify.AppId = reqMap["sub_appid"]
			payNotify.MchId = reqMap["sub_mch_id"]
		} else {
			payNotify.AppId = reqMap["appid"]
			payNotify.MchId = reqMap["mch_id"]
		}
		payNotify.OrderId = reqMap["out_trade_no"]
		payNotify.TransactionId = reqMap["transaction_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		if _, ok := reqMap["cash_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["cash_fee"], 32); err == nil {
				payNotify.CashFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
		if _, ok := reqMap["coupon_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["coupon_fee"], 32); err == nil {
				payNotify.CouponFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
		payNotify.FeeType = reqMap["fee_type"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		if len(reqMap["sub_mch_id"]) > 0 {
		    payNotify.MchTypeId = 2
		}else{
		    payNotify.MchTypeId = 1
		}
		if err = OrderPayNotify(payNotify); err != nil {
		}
		/*
		payNotify.AppId = "wxed6fc8dcb75dc2bb"  //为了测试
		payNotify.MchId = "1486657952"          //为了测试
		payNotify.MchTypeId = 2                 //为了测试
		acPay, e := isv.GetParamsByAppIdOrSubAppId(payNotify.AppId)
		if e != nil {
            beego.Debug(e.Error())
		}
		if acPay.MchTypeId == 2 {
		    wxpay.OrderQuery(acPay.AppId,acPay.MchId,acPay.SubAppId,acPay.SubMchId,acPay.Appkey,"",reqMap["out_trade_no"])
			wxpay.DownloadBill(acPay.AppId,acPay.MchId,acPay.SubAppId,acPay.SubMchId,acPay.Appkey)
		}else{
		
		    wxpay.OrderQuery(acPay.AppId,acPay.MchId,"","",acPay.Appkey,"",reqMap["out_trade_no"])
			wxpay.DownloadBill(acPay.AppId,acPay.MchId,"","",acPay.Appkey)
		}
		*/
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		if len(reqMap["sub_mch_id"]) > 0 {    
			payNotify.AppId = reqMap["sub_appid"]
			payNotify.MchId = reqMap["sub_mch_id"]
		} else {
			payNotify.AppId = reqMap["appid"]
			payNotify.MchId = reqMap["mch_id"]
		}
		payNotify.OrderId = reqMap["out_trade_no"]
        payNotify.TransactionId = reqMap["transaction_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		if _, ok := reqMap["cash_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["cash_fee"], 32); err == nil {
				payNotify.CashFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
		if _, ok := reqMap["coupon_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["coupon_fee"], 32); err == nil {
				payNotify.CouponFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
		payNotify.FeeType = reqMap["fee_type"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		if len(reqMap["sub_mch_id"]) > 0 {
		    payNotify.MchTypeId = 2
		}else{
		    payNotify.MchTypeId = 1
		}
		if err = ChargeNotify(payNotify); err != nil {
		    beego.Debug("ChargeNotify err:", err.Error())
		}
	} else {
	    if len(reqMap["sub_mch_id"]) > 0 {    
			payNotify.AppId = reqMap["sub_appid"]
			payNotify.MchId = reqMap["sub_mch_id"]
			payNotify.OpenId = reqMap["sub_openid"]
		} else {
			payNotify.AppId = reqMap["appid"]
			payNotify.MchId = reqMap["mch_id"]
			payNotify.OpenId = reqMap["openid"]
		}
		payNotify.ErrCodeDes = reqMap["return_msg"]
		payNotify.FeeType = reqMap["fee_type"]
		payNotify.TransactionId = reqMap["transaction_id"]
		payNotify.OrderId = reqMap["out_trade_no"]
		payNotify.Kind = reqMap["attach"]
		payNotify.TimeEnd = reqMap["time_end"]
		payNotify.ResultCode = reqMap["result_code"]
		payNotify.ErrCode = reqMap["err_code"]
		payNotify.ErrCodeDes = reqMap["err_code_des"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		if _, ok := reqMap["cash_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["cash_fee"], 32); err == nil {
				payNotify.CashFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
		if _, ok := reqMap["coupon_fee"]; ok {
		    if tmpAmount, err := strconv.ParseFloat(reqMap["coupon_fee"], 32); err == nil {
				payNotify.CouponFee = math.DivTotalFee(float32(tmpAmount), 100)
			}
		}
        ComPayNotify(payNotify)
	}
	return nil
}

func BalancePayHandle(reqMap map[string]string) (err error) {
	if err = BalancePayMsg(reqMap["out_trade_no"]); err != nil {
	}	
	return nil
}

func MsgRoyPayHandle(reqMap map[string]string) (err error) {
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["partner_order_id"]
		payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["real_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["partner_order_id"]
        payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["partner_order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.TransactionId = reqMap["order_id"]
		payNotify.TimeEnd = reqMap["time"]
		payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgYabandPayHandle(reqMap map[string]string) (err error) {
	if reqMap["state"] != "paid" {
	    beego.Debug("yabandpay no need process message")
	    return nil
	}
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["order_id"]
		payNotify.TransactionId = reqMap["trade_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["order_id"]
        payNotify.TransactionId = reqMap["trade_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.TransactionId = reqMap["trade_id"]
		payNotify.TimeEnd = reqMap["createDate"]
		payNotify.FeeType = reqMap["currency"]
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgSuperPayHandle(reqMap map[string]string) (err error) {
	if reqMap["trade_status"] != "TRADE_SUCCESS" {
	    beego.Debug("superpay no need process message")
	    return nil
	}
	tmpValue := strings.TrimSpace(reqMap["trade_amount"])
	arr := strings.Split(tmpValue, " ")
	reqMap["trade_amount"] = arr[0]
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["outer_trade_no"]
		payNotify.TransactionId = reqMap["inner_trade_no"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trade_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["outer_trade_no"]
        payNotify.TransactionId = reqMap["inner_trade_no"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trade_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["outer_trade_no"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trade_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.TransactionId = reqMap["inner_trade_no"]
		payNotify.TimeEnd = reqMap["gmt_create"]
		payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgUsagmsHandle(reqMap map[string]string) (err error) {
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["partner_order_id"]
		payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["real_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["partner_order_id"]
        payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["partner_order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["total_fee"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.TransactionId = reqMap["order_id"]
		payNotify.TimeEnd = reqMap["time"]
		payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgDahlPayHandle(reqMap map[string]string) (err error) {
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["ext"]
		payNotify.TransactionId = reqMap["flowid"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["fee"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		//payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["ext"]
        payNotify.TransactionId = reqMap["flowid"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["fee"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		//payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["ext"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["fee"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.TransactionId = reqMap["flowid"]
		payNotify.TimeEnd = reqMap["paytime"]
		//payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgWiseCashierHandle(reqMap map[string]string) (err error) {
	if reqMap["status"] != "PAID" {
	    beego.Debug("wisecashier no need process message")
	    return nil
	}
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["agent_order_id"]
		//payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["agent_order_id"]
        //payNotify.TransactionId = reqMap["order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
        payNotify.OrderId = reqMap["agent_order_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		//payNotify.TransactionId = reqMap["order_id"]
		payNotify.TimeEnd = reqMap["time"]
		payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgMegaezgoHandle(reqMap map[string]string) (err error) {
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["STORE_ORDER_NO"]
		payNotify.TransactionId = reqMap["SYS_ORDER_ID"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["ORDER_AMOUNT"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["ORDER_CURRENCY"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["STORE_ORDER_NO"]
        payNotify.TransactionId = reqMap["SYS_ORDER_ID"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["ORDER_AMOUNT"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["ORDER_CURRENCY"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["STORE_ORDER_NO"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["ORDER_AMOUNT"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.TransactionId = reqMap["SYS_ORDER_ID"]
		payNotify.TimeEnd = reqMap["ORDER_TIME"]
		payNotify.FeeType = reqMap["ORDER_CURRENCY"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgChinaUmsHandle(reqMap map[string]string) (err error) {
	if reqMap["status"] != "TRADE_SUCCESS" {
	    beego.Debug("chinaums no need process message")
	    return nil
	}
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["merOrderId"]
		payNotify.TransactionId = reqMap["seqId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["buyerPayAmount"], 32); err == nil {
			payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["merOrderId"]
        payNotify.TransactionId = reqMap["seqId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["buyerPayAmount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["merOrderId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["buyerPayAmount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.TransactionId = reqMap["seqId"]
		payNotify.TimeEnd = reqMap["payTime"]
		//payNotify.FeeType = reqMap["currency"] 
		payNotify.Kind = reqMap["attach"]
		if reqMap["status"] == "TRADE_SUCCESS" {
		    payNotify.ResultCode = "SUCCESS"
		}
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgOceanPayHandle(reqMap map[string]string) (err error) {
	if reqMap["payment_status"] != "1" {
	    beego.Debug("oceanpay no need process message")
	    return nil
	}
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["order_number"]
		payNotify.TransactionId = reqMap["payment_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["order_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["order_currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["order_number"]
        payNotify.TransactionId = reqMap["payment_id"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["order_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["order_currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["order_number"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["order_amount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.TransactionId = reqMap["payment_id"]
		payNotify.TimeEnd = reqMap["payTime"]
		payNotify.FeeType = reqMap["order_currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func MsgQcurePayHandle(reqMap map[string]string) (err error) {
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["merchOrderNo"]
		payNotify.TransactionId = reqMap["orderNo"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["order_currency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["merchOrderNo"]
        payNotify.TransactionId = reqMap["orderNo"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["order_currency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["merchOrderNo"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["amount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.TransactionId = reqMap["orderNo"]
		payNotify.TimeEnd = reqMap["payTime"]
		//payNotify.FeeType = reqMap["order_currency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func HnaPayHandle(reqMap map[string]string) (err error) {
	/*
	for k, v := range reqMap {
	   beego.Debug("HnaPayHandle k:",k,"v:",v)
	}
	*/
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["merOrderId"]
		payNotify.TransactionId = reqMap["dealId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["payCurrency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["merOrderId"]
        payNotify.TransactionId = reqMap["dealId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.FeeType = reqMap["payCurrency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["merOrderId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.ResultCode = "SUCCESS"
		payNotify.PayWayId = 20
		payNotify.TransactionId = reqMap["dealId"]
		payNotify.TimeEnd = reqMap["completeTime"]
		payNotify.FeeType = reqMap["payCurrency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func AllInPayHandle(reqMap map[string]string) (err error) {
	/*
	for k, v := range reqMap {
	   beego.Debug("AllInPayHandle k:",k,"v:",v)
	}
	*/
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["cusorderid"]
		payNotify.TransactionId = reqMap["chnltrxid"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trxamt"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["payCurrency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["cusorderid"]
        payNotify.TransactionId = reqMap["chnltrxid"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trxamt"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		//payNotify.FeeType = reqMap["payCurrency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["cusorderid"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["trxamt"], 32); err == nil {
		    payNotify.TotalFee = math.DivTotalFee(float32(tmpAmount), 100)
		}
		payNotify.TransactionId = reqMap["chnltrxid"]
		payNotify.TimeEnd = reqMap["paytime"]
		//payNotify.FeeType = reqMap["payCurrency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}

func GpslPayHandle(reqMap map[string]string) (err error) {
	/*
	for k, v := range reqMap {
	   beego.Debug("HnaPayHandle k:",k,"v:",v)
	}
	*/
	payNotify := new(CommonPayNotify)
	if reqMap["attach"] == "ESHOP_ORDER_PAY" {
		payNotify.OrderId = reqMap["merOrderId"]
		payNotify.TransactionId = reqMap["dealId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["payCurrency"]
		if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		OrderPayNotify(payNotify)
	} else if reqMap["attach"] == "MEMBER_CHARGE" {
		payNotify.OrderId = reqMap["merOrderId"]
        payNotify.TransactionId = reqMap["dealId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.FeeType = reqMap["payCurrency"]
        if tmpBody, err := json.Marshal(&reqMap); err == nil {
		    payNotify.SrcMessage = string(tmpBody)
	    }
		ChargeNotify(payNotify)
		
	} else {
		payNotify.OrderId = reqMap["merOrderId"]
		if tmpAmount, err := strconv.ParseFloat(reqMap["payAmount"], 32); err == nil {
		    payNotify.TotalFee = float32(tmpAmount)
		}
		payNotify.ResultCode = "SUCCESS"
		payNotify.TransactionId = reqMap["dealId"]
		payNotify.TimeEnd = reqMap["completeTime"]
		payNotify.FeeType = reqMap["payCurrency"] 
		payNotify.Kind = reqMap["attach"]
		ComPayNotify(payNotify)
	}
	return nil
}
